﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections;

namespace Goldlight.Silverlight.Mvvm.Base.Infrastructure
{
    public class CollectionViewSource
    {
        public static ICollectionView GetDefaultView(object source)
        {
            return GetOriginalView(GetDefaultCollectionView(source, true));
        }

        internal static CollectionView GetDefaultCollectionView(object source, bool createView)
        {
            if (!IsValidSourceForView(source))
            {
                return null;
            }
            ViewRecord record = DataBindEngine.CurrentDataBindEngine.GetViewRecord(source, DefaultSource, null, createView);
            if (record == null)
            {
                return null;
            }
            return (CollectionView)record.View;
        }

        private static bool IsValidSourceForView(object o)
        {
            if ((o != null) && !(o is IEnumerable))
            {
                return (o is IListSource);
            }
            return true;
        }


    }
}
